<template>
  <div class="contact-container">
    <!-- banner   START -->
    <div class="container-fluild">
      <div class="row long-row">
        <span class="title">联系我们</span>
        <img src="./images/slide-contact.png" alt="contact" />
      </div>
    </div>
    <!-- banner   END -->
    <!-- contact-detail  START -->
    <div class="container long-container wow animate__animated animate__fadeInUp" data-wow-duration="2s">
      <div class="row">
        <div class="col-lg-4 root">
          <div class="inside-root">
            <span class="icon">
              <img src="./images/phone.png" alt="电话">
            </span>
            <h3>联系电话</h3>
            <p>Tel: 15800616566</p>
          </div>
        </div>
        <div class="col-lg-4 root">
          <div class="inside-root">
            <span class="icon">
              <img src="./images/addtress.png" alt="地址">
            </span>
            <h3>公司地址</h3>
            <p>上海市闵行区春东路508号IF如果创意产业园A栋7层</p>
          </div>
        </div>
        <div class="col-lg-4 root">
          <div class="inside-root">
            <span class="icon">
              <img src="./images/email.png" alt="邮箱">
            </span>
            <h3>公司邮箱</h3>
            <p>Mailto: info@zuofeibio.com</p>
          </div>
        </div>
      </div>
    </div>
    <!-- contact-detail  END -->

    <!-- 表单内容  START -->
    <div class="container-fluild form-container">
      <div class="container form-content wow animate__animated animate__fadeInUp" data-wow-duration="2s">
        <el-form :model="UserMsg">

          <div class="row">
            <div class="col-lg-6 long-input">
              <p><span class="red">*</span>您的姓名：</p>
              <el-form-item prop="link_man" :rules="[{ required: true, message: '姓名/昵称不能为空' }]">
                <el-input v-model="UserMsg.link_man" size="large" placeholder="请输入您的姓名/昵称" />
              </el-form-item>
            </div>

            <div class="col-lg-6 long-input">
              <p><span class="red">*</span>联系电话：</p>
              <el-form-item prop="link_mobile" :rules="[{ required: true, message: '联系电话不能为空' }]">
                <el-input v-model="UserMsg.link_mobile" size="large" placeholder="请输入您的联系电话" maxlength="11" />
              </el-form-item>
            </div>
          </div>

          <div class="row">
            <div class="col-lg-6 long-input">
              <p><span class="red">*</span>您的公司：</p>
              <el-form-item prop="company_name" :rules="[{ required: true, message: '公司名称不能为空' }]">
                <el-input v-model="UserMsg.company_name" size="large" placeholder="请输入您的公司名称" />
              </el-form-item>
            </div>
            <div class="col-lg-6 long-input">
              <p>您的邮箱：</p>
              <el-form-item>
                <el-input v-model="email" size="large" placeholder="请输入您的邮箱" />
              </el-form-item>
            </div>
          </div>

          <p>留言：</p>
          <el-form-item>
            <el-input v-model="UserMsg.details[0].special_needs" type="textarea" placeholder="请输入" rows="4" />
          </el-form-item>

          <el-button color="#4F68AC" @click="sendMsg">提交</el-button>
        </el-form>

      </div>
    </div>
    <!-- 表单内容  END -->
  </div>
</template>

<script>
  import { onMounted, nextTick, reactive, ref, computed } from "vue";
  import { WOW } from 'wowjs'
  import "animate.css";
  import { reqSendUserMsg } from '@/api'
  import { useContactStore } from "@/store/contact"  //引入news仓库
  import { storeToRefs } from 'pinia'  //引入方法让仓库里的值可以解构赋值并且具有响应式
  import { ElMessage } from 'element-plus'
  import 'element-plus/dist/index.css'
  export default {
    name: "Contact",
    setup() {
      const store = useContactStore()

      onMounted(() => {
        // if (!token.value) {
        //   store.getToken() //获取token
        // }

        //初始化wow.js(页面滚动加载动画)
        nextTick(() => {   //页面渲染完，在执行
          let wow = new WOW({
            live: false
          })
          wow.init()
        })
      });

      //数据
      let { success } = storeToRefs(store)  //从仓库获取请求状态码
      let token = computed(() => localStorage.getItem('token')) //从本地获取token
      let email = ref('')
      let UserMsg = reactive({ //收集表单的数据
        company_name: "", //公司名
        link_man: "", //联系人姓名
        link_mobile: "", //联系电话
        sale: 39, //绑定的销售
        details: [
          {
            types: 5,
            special_needs: "", // 留言
            needs_num: 1
          }
        ]
      })

      //方法
      //提交用户数据
      function sendMsg() {
        if (UserMsg.link_man && UserMsg.link_mobile && UserMsg.company_name) {//输入的name和phone不为空
          let headers = {//设置请求头，携带token
            Authorization: "Bearer " + token.value //根据服务端设置token(这里后端需要在Bearer后加上空格|不能删除)
          }
          if (UserMsg.details[0].special_needs == "") {//因为公司名是必填的参数，不填写会导致请求发送失败所以当该值为空时设置为默认值
            UserMsg.details[0].special_needs = " " //设置默认值为" "
          }

          store.sendUserMsg(UserMsg, headers) //发送请求并携带用户数据和token

          setTimeout(() => {
            if (success.value == 1) {
              //成功发送用户数据时，弹框提示成功 / type:success
              ElMessage({
                message: '提交成功',
                type: 'success',
                center: true,
              })
            } else if (success.value == 2) {
              //未成功发送用户数据时
              ElMessage({
                message: '提交失败,请刷新页面再次提交。',
                type: 'error',
                center: true,
              })
            }
          }, 100)

        }
        else {
          if (!UserMsg.link_man && UserMsg.link_mobile && UserMsg.company_name) {
            //输入的 link_man 为空时，弹框提示
            ElMessage({
              message: '您的姓名，不能为空',
              type: 'warning',
              center: true,
            })
          } else if (UserMsg.link_man && !UserMsg.link_mobile && UserMsg.company_name) {
            //输入的 link_mobile 为空时，弹框提示
            ElMessage({
              message: '联系电话，不能为空',
              type: 'warning',
              center: true,
            })
          } else if (UserMsg.link_man && UserMsg.link_mobile && !UserMsg.company_name) {
            //输入的 company_name 为空时，弹框提示
            ElMessage({
              message: '您的公司，不能为空',
              type: 'warning',
              center: true,
            })
          } else if (!UserMsg.link_man && !UserMsg.link_mobile && UserMsg.company_name) {
            //输入的 link_man 和 link_mobile 为空时，弹框提示
            ElMessage({
              message: '您的姓名、联系电话，不能为空',
              type: 'warning',
              center: true,
            })
          } else if (!UserMsg.link_man && UserMsg.link_mobile && !UserMsg.company_name) {
            //输入的 link_man 和 company_name 为空时，弹框提示
            ElMessage({
              message: '您的姓名、您的公司，不能为空',
              type: 'warning',
              center: true,
            })
          } else if (UserMsg.link_man && !UserMsg.link_mobile && !UserMsg.company_name) {
            //输入的 link_man 和 company_name 为空时，弹框提示
            ElMessage({
              message: '联系电话、您的公司，不能为空',
              type: 'warning',
              center: true,
            })
          } else {
            //输入的 link_man 和 link_mobile 和 company_name 为空时，弹框提示
            ElMessage({
              message: '您的姓名、联系电话、您的公司，不能为空',
              type: 'warning',
              center: true,
            })
          }

        }
      }

      return {
        email,
        UserMsg,
        sendMsg
      }
    },
  };
</script>

<style>
  /* banner  START */
  .contact-container .long-row {
    position: relative;
    overflow: hidden;
  }

  .contact-container .long-row .title {
    position: absolute;
    z-index: 9;
    top: 50%;
    margin-top: -26px;
    left: 50%;
    margin-left: -80px;
    font-size: 40px;
    font-family: "Source Han Sans CN";
    font-weight: 300;
    color: #fff;
    user-select: none;
  }

  .contact-container .long-row img {
    width: 100%;
    transition: transform 2s linear;
  }

  .contact-container .long-row:hover img {
    transform: scale(1.12);
  }

  /* banner  END */

  /* contact-detail  START  */
  .contact-container .long-container {
    position: relative;
  }

  .contact-container .long-container .root {
    padding: 0px 10px;
  }

  .contact-container .long-container .root .inside-root {
    background-color: rgba(255, 255, 255, 0.8);
    height: 250px;
    border-radius: 10px;
    padding: 50px 30px;
    cursor: pointer;
    box-shadow:
      0px 0px 1.4px -15px rgba(0, 0, 0, 0.031),
      0px 0px 3.5px -15px rgba(0, 0, 0, 0.044),
      0px 0px 7.1px -15px rgba(0, 0, 0, 0.056),
      0px 0px 14.6px -15px rgba(0, 0, 0, 0.069),
      0px 0px 40px -15px rgba(0, 0, 0, 0.2);
    transition: transform 0.5s ease;
  }

  .contact-container .long-container .root .icon {
    width: 50px;
    height: 50px;
    display: block;
    margin: 0 auto;
  }

  .contact-container .long-container .root .icon img {
    width: 100%;
    height: 100%;
  }

  .contact-container .long-container .root h3 {
    font-family: "Source Han Sans CN";
    font-size: 16px;
    color: #999;
    text-align: center;
    margin-top: 20px;
    line-height: 25px;
  }

  .contact-container .long-container .root p {
    font-family: "Source Han Sans CN";
    font-size: 15px;
    color: #555;
    text-align: center;
    margin-top: 5px;
    line-height: 25px;
  }

  /* contact-detail  END */

  /* 表单内容  START */
  .contact-container .form-container {
    background-color: #f8f8f8;
    padding-top: 60px;
    padding-bottom: 80px;
  }

  .contact-container .form-container .form-content {
    background-color: #fff;
    border-radius: 10px;
    padding: 60px 50px;

  }

  .contact-container .form-content p {
    font-size: 16px;
    font-family: "Source Han Sans CN";
    margin-bottom: 10px;
    color: #666;
  }

  .contact-container .long-input .red {
    color: red;
  }


  /* element input */
  .contact-container .el-input__inner {
    font-size: 15px;
    color: #333;
    font-family: "Source Han Sans CN";
    height: 50px;
    margin-bottom: 30px;
  }

  .contact-container .el-input__inner:focus {
    border: 1px solid #4F68AC;
  }

  .contact-container .el-textarea__inner {
    font-size: 15px;
    color: #333;
    font-family: "Source Han Sans CN";
  }

  .contact-container .el-textarea__inner:focus {
    border: 1px solid #4F68AC;
  }

  .contact-container .el-button {
    position: relative;
    left: 50%;
    margin-left: -145px;
    width: 290px;
    height: 50px;
    font-size: 16px;
    font-family: "Source Han Sans CN";
    margin-top: 40px;
  }

  .contact-container .el-form-item__error {
    font-size: 14px;
    top: 70%;
    font-family: "Source Han Sans CN";
  }


  /* 表单内容  END */

  /* 设置响应式 */
  /* 移动端 */
  @media (max-width: 1200px) {
    .contact-container .long-container {
      top: 20px;
      padding-bottom: 120px;
    }

    .contact-container .long-container .root .inside-root {
      margin-top: 30px;
    }
  }


  /* PC端 */
  @media (min-width: 1200px) {
    .contact-container .long-container {
      top: -120px;
    }

    .contact-container .long-container .root .inside-root:hover {
      transform: translateY(-50px)
    }
  }
</style>